from PyQt5.QtWidgets import QSpacerItem


class ComponentUtil:
    """
    封装控件相关的工具类
    """
    @staticmethod
    def delete_all(this_layout) -> None:
        """
            删除布局下的所有控件，包括弹簧控件,
            可抽取到工具类
        """
        item_list = list(range(this_layout.count()))
        item_list.reverse()  # 倒序删除，避免影响布局顺序

        for i in item_list:
            item = this_layout.itemAt(i)
            if item is not None:
                if item.widget() is not None:
                    item.widget().deleteLater()
                elif isinstance(item, QSpacerItem):
                    this_layout.removeItem(item)
                else:
                    self.delete_all(item.layout())
                this_layout.removeItem(item)